#pragma once
#include <iostream>
#include <string>
#include <mutex>
#include <fstream>
#include <iomanip>

// 文件名用于保存 currentBID
const std::string BID_FILE = "generateBID.txt";

static int currentBID = 0;
std::mutex bidMutex;

// Function to load currentBID from file
void loadCurrentBID()
{
    std::ifstream file(BID_FILE);
    if (file.is_open())
    {
        file >> currentBID;
        file.close();
    }
    else
    {
        // 文件不存在或无法打开，使用初始值
        currentBID = 0;
    }
}

// Function to save currentBID to file
void saveCurrentBID()
{
    std::ofstream file(BID_FILE);
    if (file.is_open())
    {
        file << currentBID;
        file.close();
    }
}

// Function to generate a unique BID
std::string generateBID()
{
    std::lock_guard<std::mutex> lock(bidMutex);

    currentBID++;
    std::stringstream ss;
    ss << std::setw(6) << std::setfill('0') << currentBID;

    saveCurrentBID(); // 保存更新后的 currentBID

    return ss.str();
}

// int main()
// {
//     loadCurrentBID(); // 启动时加载 currentBID

//     // Example of generating BIDs
//     std::cout << "BID: " << generateBID() << std::endl;
//     std::cout << "BID: " << generateBID() << std::endl;

//     return 0;
// }
